package com.rain.cy.business.converter;

import cn.hutool.json.JSONUtil;
import com.rain.cy.business.constant.CommonConstant;
import com.rain.cy.business.handler.*;
import com.rain.cy.business.handler.base.ProcessHandler;
import com.rain.cy.business.model.dto.UserProcessDataDTO;
import com.rain.cy.business.model.json.*;
import com.rain.cy.framework.utils.SpringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yu_chen
 * @since 2021/10/7 13:54
 */
public class UserProcessDataConverter {

    public static final Map<String, Class<?>> CLASS_MAP = new HashMap<>();

    public static final Map<String, ProcessHandler> HANDLER_MAP = new HashMap<>();

    static {
        CLASS_MAP.put(CommonConstant.PROCESS_ID_ONE, ProcessOneOfDataJson.class);
        CLASS_MAP.put(CommonConstant.PROCESS_ID_TWO, ProcessTwoOfDataJson.class);
        CLASS_MAP.put(CommonConstant.PROCESS_ID_THREE, ProcessThreeOfDataJson.class);
        CLASS_MAP.put(CommonConstant.PROCESS_ID_FOUR, ProcessFourOfDataJson.class);
        CLASS_MAP.put(CommonConstant.PROCESS_ID_FIVE, ProcessFiveOfDataJson.class);
        CLASS_MAP.put(CommonConstant.PROCESS_ID_SIX, ProcessSixOfDataJson.class);

        HANDLER_MAP.put(CommonConstant.PROCESS_ID_ONE, SpringUtil.getBean(ProcessOneHandler.class));
        HANDLER_MAP.put(CommonConstant.PROCESS_ID_TWO,  SpringUtil.getBean(ProcessTwoHandler.class));
        HANDLER_MAP.put(CommonConstant.PROCESS_ID_THREE, SpringUtil.getBean(ProcessThreeHandler.class));
        HANDLER_MAP.put(CommonConstant.PROCESS_ID_FOUR,  SpringUtil.getBean(ProcessFourHandler.class));
        HANDLER_MAP.put(CommonConstant.PROCESS_ID_FIVE,  SpringUtil.getBean(ProcessFiveHandler.class));
    }


    public static UserProcessDataDTO initBaseProcessDataJson(UserProcessDataDTO userProcessDataDTO) {
        String processData = userProcessDataDTO.getProcessData();
        if (StringUtils.isEmpty(processData)){
            return null;
        }
        BaseProcessDataJson baseProcessDataJson = (BaseProcessDataJson) JSONUtil.toBean(processData, CLASS_MAP.get(userProcessDataDTO.getProcessId()));
        userProcessDataDTO.setBaseProcessDataJson(baseProcessDataJson);
        return userProcessDataDTO;
    }
}
